package com.koron.tool.frame;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.List;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import org.koron.ebs.util.Utils;

import com.koron.common.KVBean;
import com.koron.localdb.LocalMapper;
import com.koron.mybatis.SessionFactory;
import com.koron.tool.Constant;
import com.koron.tool.MainFrame;
import com.koron.ui.MutableGridLayout;

public class ProjectPropertiesFrame extends JInternalFrame {
	private DefaultTableModel dtm = new DefaultTableModel(new String[] { "属性", "值" }, 0);
	private JTable jtb = new JTable(dtm);

	public ProjectPropertiesFrame() {
		super("项目属性");
		setSize(600, 400);
		setMaximizable(true);
		setClosable(true);
		setIconifiable(true);
		setResizable(true);
		MutableGridLayout layout = new MutableGridLayout(2, 2, 5, 5);
		layout.merge(0, 0, 1, 0);
		layout.setHeight(1, 30);
		setLayout(layout);
		jtb.getColumnModel().getColumn(0).setCellRenderer(Constant.getTableRender());
		JScrollPane jsp = new JScrollPane(jtb);
		jtb.setRowHeight(30);
		jtb.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				if (jtb.getSelectedRow() != -1 && jtb.getSelectedRow() + 1 == dtm.getRowCount()) {
					dtm.setRowCount(dtm.getRowCount() + 1);
				}
			}
		});
		jsp.setPreferredSize(new Dimension(10, 10));
		add(jsp);
		add(new JButton(new SaveAction(this, dtm,jtb)));
		loadTable();
		setVisible(true);
	}

	public void loadTable() {
		SessionFactory factory = new SessionFactory();
		List<KVBean> list = factory.getMapper(LocalMapper.class).listProperties();
		factory.close();
		dtm.setRowCount(0);
		for (KVBean projectBean : list) {
			dtm.addRow(new String[] { projectBean.getKey(), projectBean.getValue() });
		}
	}

	class SaveAction extends AbstractAction {
		ProjectPropertiesFrame frame;
		DefaultTableModel dtm;
		JTable jtb;


		public SaveAction(ProjectPropertiesFrame frame, DefaultTableModel dtm,JTable jtb) {
			putValue(NAME, "保存");
			putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
			this.frame = frame;
			this.dtm = dtm;
			this.jtb = jtb;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			SessionFactory factory = new SessionFactory();
			LocalMapper mapper = factory.getMapper(LocalMapper.class);
			mapper.deleteProperties();
			if(jtb.isEditing()){
				jtb.getCellEditor().stopCellEditing();
			}

			int size = dtm.getRowCount();

			for (int i = 0; i < size; i++) {
				KVBean bean = new KVBean();
				if (dtm.getValueAt(i, 0) != null) {
					bean.setKey(dtm.getValueAt(i, 0).toString());
					bean.setValue(Utils.isNull(dtm.getValueAt(i, 1), dtm.getValueAt(i, 1).toString(), ""));
					if (bean.getKey() != null && !bean.getKey().isEmpty())
						mapper.addProperty(bean);
				}
			}
			factory.close();
			frame.loadTable();
			MainFrame.loadConfig();
		}
	}
}
